package com.ckx;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenkaixiang on 2017-12-14.
 */
public class NodeDemo {

    private static Map<Integer, Node> DB = new LinkedHashMap<>();
    private static List<Node> childs = new ArrayList<>();

    static {
        Node rootNode = new Node(1, "rootNode", 0, true, null);
        Node child01 = new Node(2, "child01", 1, false, "0-1");
        Node child02 = new Node(3, "child02", 1, false, "0-2");
        DB.put(rootNode.getNodeId(), rootNode);
        DB.put(child01.getNodeId(), child01);
        DB.put(child02.getNodeId(), child02);
        childs.add(child01);
        childs.add(child02);
    }

    public String build() {
        //调用构建子节点的函数
        Node node = DB.get(1);
        buildNode(node);
        return node.toString();
    }

    /**
     * 构建树形节点的内容
     */
    private void buildNode(Node node) {
        //查看此节点的子节点
        boolean hasChild = node.isHasChild();
        //如果有子节点，进行递归调用，调用自己这个函数
        if (hasChild) {
            Integer parentId = node.getParentId();
            // sql 查询 parent为该节点的node
            List<Node> nodes = childs;
            node.setChildNodes(childs);
            for (Node n : nodes) {
                buildNode(n);
            }
        }
    }

    public static void main(String[] args) {
        NodeDemo demo = new NodeDemo();
        String str = demo.build();
        System.out.println(str);
    }


}

class Node {

    private Integer nodeId;
    private String nodeName;
    private Integer parentId;
    private boolean hasChild;
    private String nodePath;
    private List<Node> childNodes;

    public Node(Integer nodeId, String nodeName, Integer parentId, boolean hasChild, String nodePath) {
        this.nodeId = nodeId;
        this.nodeName = nodeName;
        this.parentId = parentId;
        this.hasChild = hasChild;
        this.nodePath = nodePath;
    }

    public Integer getNodeId() {
        return nodeId;
    }
    public void setNodeId(Integer nodeId) {
        this.nodeId = nodeId;
    }

    public String getNodeName() {
        return nodeName;
    }
    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public Integer getParentId() {
        return parentId;
    }
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public boolean isHasChild() {
        return hasChild;
    }
    public void setHasChild(boolean hasChild) {
        this.hasChild = hasChild;
    }

    public String getNodePath() {
        return nodePath;
    }
    public void setNodePath(String nodePath) {
        this.nodePath = nodePath;
    }

    public List<Node> getChildNodes() {
        return childNodes;
    }
    public void setChildNodes(List<Node> childNodes) {
        this.childNodes = childNodes;
    }

    @Override
    public String toString() {
        return "Node{" +
            "nodeId=" + nodeId +
            ", nodeName='" + nodeName + '\'' +
            ", parentId=" + parentId +
            ", hasChild=" + hasChild +
            ", nodePath='" + nodePath + '\'' +
            ", childNodes=" + childNodes +
            '}';
    }
}
